#!/usr/bin/env python

""" Example for analyse URIs over a given domain to monitor """

__author__ = "Luis Campo Giralte"
__copyright__ = "Copyright (C) 2013-2020 by Luis Campo Giralte"
__revision__ = "$Id$"
__version__ = "0.1"

import sys
import base64
import glob
sys.path.append("../src/")
import pyaiengine

def callback_uri(flow):

    h = flow.http_info
    if (h):
        uri = str(h.uri)
        if (uri):
            if ((uri.startswith("http") == False)and(uri[0] != "/")):
                if (uri.find('%') >= 0):
                    # The URI is encoded, so decode for analysis
                    try:
                        urie = base64.b64decode(uri)
                        print "Original:",uri
                        print "Decoded:",urie
                    except Exception as e:
                        pass 

if __name__ == '__main__':

    # Load an instance of a Network Stack on mobile network
    st = pyaiengine.StackMobile()

    # Create a instace of a PacketDispatcher
    pdis = pyaiengine.PacketDispatcher()

    # Plug the stack on the PacketDispatcher
    pdis.stack = st

    dm = pyaiengine.DomainNameManager()

    dom = pyaiengine.DomainName("Service to analyze",
        "facebook.com")
    dom.callback = callback_uri
    dm.add_domain_name(dom)
    
    st.set_domain_name_manager(dm,"http")

    st.tcp_flows = 327680
    st.udp_flows = 163840

    files = glob.glob("/home/luis/pcapfiles/examples/*.pcap")
    for f in files:
        pdis.open(f)
        pdis.run()

    pdis.close()

    sys.exit(0)

